package com.hzyc.springdata.jpa.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzyc.springdata.jpa.domain.PageBean;
import com.hzyc.springdata.jpa.domain.User;
import com.hzyc.springdata.jpa.service.IUserService;

@Controller
public class UserController {

	@Resource
	private IUserService userService;
	
	@RequestMapping("/getUser")
	@ResponseBody
	public User getUser(long id){
		return userService.get(id);
	}
	
	@CrossOrigin(methods = { RequestMethod.GET, RequestMethod.POST }, origins = "*")
	@RequestMapping("/getByPage")
	@ResponseBody
	public PageBean<User> getByPage(int page, int rows){
		Page<User> p = userService.getByPage(page-1, rows);
		PageBean<User> pagebean = new PageBean<User>(p.getTotalElements(), p.getContent());
		return pagebean;
	}
	
	@RequestMapping("/findByUserNameAndPassword")
	@ResponseBody
	public List<User> findByUserNameAndPassword(String userName, String password){
		return userService.findByUserNameAndPassword(userName, password);
	}


	@RequestMapping("/findByRoleOrderByIdDesc")
	@ResponseBody
	public List<User> findByRoleOrderByIdDesc(String role){
		return userService.findByRoleOrderByIdDesc(role);
	}

	@RequestMapping("/countByName")
	@ResponseBody
	public Long countByName(String name){
		return userService.countByUserName(name);
	}

	@RequestMapping("/findByRole")
	@ResponseBody
	public Page<User> findByRole(String role,int page,int size){
		return userService.findByRole(role, page, size);
	}
	
	@RequestMapping("/findByRole2")
	@ResponseBody
	public List<User> findByRole(String role){
		return userService.findByRole(role);
	}
	
	@RequestMapping("/queryUser")
	@ResponseBody
	public List<User> queryUser(String userName){
		return userService.queryUser(userName);
	}
	

	@RequestMapping("/login")
	@ResponseBody
	public User login(String userName,String pwd){
		return userService.login(userName, pwd);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public int setUserNameById(String userName, int id){
		int i= userService.setUserNameById(userName, id);
		System.out.println(i);
		return i;
	}
}
